rm(list = ls())
library(foreign)
library(Synth)

#set working directory
source('functions.R')
dat <- read.csv('raw_ts.csv')
dat <- dat[order(dat$Country, dat$Year), ]
dat <- subset(dat, Year > 1880 & Year <= 2015)
dset <- subset(dat, cus_AR == 1 & cus_syear > 1900 & cus_t == 1)


#Exclude revolutionary regimes from donor pool
revregimes <- c(
  'Albania 1944-1991',
  'Afghanistan 1996-2001',
  'Algeria 1962-',
  'Angola 1975-',
  'Bolivia 1952-1964',
  'Cambodia 1975-1979',
  'China 1949-',
  'Cuba 1959-',
  'Eritrea 1993-',
  'Guinea-Bissau 1974-1999',
  'Iran 1979-',
  'Mexico 1915-2000',
  'Mozambique 1975-',
  'Nicaragua 1979-1990',
  'Rwanda 1994-',
  'Russia 1917-1991',
  'Vietnam 1954-',
  'Yugoslavia 1945-1990'
)

##################################################################
#Civil Society Strength
##################################################################
dep_var <- 'v2xcs_ccsi'

#------------------------------------
#Afghanistan
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(d = dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Afghanistan'|new_d$cus_caseid == 'Afghanistan 1996-2001',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Afghanistan 1996-2001'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Afghanistan <- dataprep(new_d,
                                    dependent = dep_var,
                                    unit.variable = 'case_num',
                                    time.variable = 't',
                                    special.predictors = list(
                                      list("reg.civsoc.lnd", -1:-4, "mean"),
                                      list("reg.civsoc.lnd", -5:-8, "mean"),
                                      list("reg.civsoc.lnd", -9:-12, "mean"),
                                      list("v2xel_frefair", -1:-4, "mean"),
                                      list("v2xel_frefair", -5:-8, "mean"),
                                      list("v2xel_frefair", -9:-12, "mean"),
                                      list("v2xcs_ccsi", -1:-4, "mean"),
                                      list("v2xcs_ccsi", -5:-8, "mean"),
                                      list("v2xcs_ccsi", -9:-12, "mean")
                                    ),
                                    treatment.identifier = treatment.identifier,
                                    controls.identifier = as.integer(control_id),
                                    time.optimize.ssr = c(-12:-1),
                                    time.predictors.prior = c(-12:-1),
                                    unit.names.variable = 'cus_caseid',
                                    time.plot = c(-12:10)
)

synth.civsoc.Afghanistan <- synth(data.civsoc.Afghanistan, custom.v = rep(1, 9))
path.plot(synth.civsoc.Afghanistan, data.civsoc.Afghanistan)

#------------------------------------
#Abania
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd' #incpmplete time-series
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[(new_d$Country != 'Albania')|(new_d$cus_caseid == 'Albania 1944-1991'),]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Albania 1944-1991'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Albania <- dataprep(new_d,
                                #predictors = pred_vars,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                # = c('reg.civsoc.lnd', 'v2xel_frefair'),
                                special.predictors = list(
                                  #list("reg.civsoc.lnd", -1:-4, "mean"),
                                  #list("reg.civsoc.lnd", -5:-8, "mean"),
                                  #list("reg.civsoc.lnd", -9:-12, "mean"),
                                  list("v2xel_frefair", -1:-4, "mean"),
                                  list("v2xel_frefair", -5:-8, "mean"),
                                  list("v2xel_frefair", -9:-12, "mean"),
                                  list("v2xcs_ccsi", -1:-4, "mean"),
                                  list("v2xcs_ccsi", -5:-8, "mean"),
                                  list("v2xcs_ccsi", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-12:-1),
                                time.predictors.prior = c(-12:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-12:10)
)
synth.civsoc.Albania <- synth(data.civsoc.Albania, custom.v = rep(1, 6))#, optimxmethod  ='L-BFGS-B')#)
path.plot(synth.civsoc.Albania, data.civsoc.Albania)

#------------------------------------
#Algeria
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Algeria' | new_d$cus_caseid == 'Algeria 1962-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Algeria 1962-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Algeria <- dataprep(new_d,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                special.predictors = list(
                                  #list("reg.civsoc.lnd", -1:-4, "mean"),
                                  #list("reg.civsoc.lnd", -5:-8, "mean"),
                                  #list("reg.civsoc.lnd", -9:-12, "mean"),
                                  list("v2xel_frefair", -1:-4, "mean"),
                                  list("v2xel_frefair", -5:-8, "mean"),
                                  list("v2xel_frefair", -9:-12, "mean"),
                                  list("v2xcs_ccsi", -1:-4, "mean"),
                                  list("v2xcs_ccsi", -5:-8, "mean"),
                                  list("v2xcs_ccsi", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-12:-9),
                                time.predictors.prior = c(-12:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-12:10)
)
synth.civsoc.Algeria <- synth(data.civsoc.Algeria, custom.v = rep(1/6, 6))
path.plot(synth.civsoc.Algeria, data.civsoc.Algeria)

#------------------------------------
#Angola
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Angola'|new_d$cus_caseid == 'Angola 1975-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Angola 1975-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Angola <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 #list("reg.civsoc.lnd", -1:-4, "mean"),
                                 #list("reg.civsoc.lnd", -5:-8, "mean"),
                                 #list("reg.civsoc.lnd", -9:-12, "mean"),
                                 list("v2xel_frefair", -1:-4, "mean"),
                                 list("v2xel_frefair", -5:-8, "mean"),
                                 list("v2xel_frefair", -9:-12, "mean"),
                                 list("v2xcs_ccsi", -1:-4, "mean"),
                                 list("v2xcs_ccsi", -5:-8, "mean"),
                                 list("v2xcs_ccsi", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.civsoc.Angola <- synth(data.civsoc.Angola, custom.v = rep(1, 6))
path.plot(synth.civsoc.Angola, data.civsoc.Angola)

#------------------------------------
#Bolivia
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Bolivia'|new_d$cus_caseid == 'Bolivia 1952-1964',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Bolivia 1952-1964'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Bolivia <- dataprep(new_d,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                special.predictors = list(
                                  list("reg.civsoc.lnd", -1:-4, "mean"),
                                  list("reg.civsoc.lnd", -5:-8, "mean"),
                                  list("reg.civsoc.lnd", -9:-12, "mean"),
                                  list("v2xel_frefair", -1:-4, "mean"),
                                  list("v2xel_frefair", -5:-8, "mean"),
                                  list("v2xel_frefair", -9:-12, "mean"),
                                  list("v2xcs_ccsi", -1:-4, "mean"),
                                  list("v2xcs_ccsi", -5:-8, "mean"),
                                  list("v2xcs_ccsi", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-12:-1),
                                time.predictors.prior = c(-12:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-12:10)
)

synth.civsoc.Bolivia <- synth(data.civsoc.Bolivia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Bolivia, data.civsoc.Bolivia)

#------------------------------------
#Cambodia
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Cambodia'|new_d$cus_caseid == 'Cambodia 1975-1979',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Cambodia 1975-1979'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Cambodia <- dataprep(new_d,
                                 dependent = dep_var,
                                 unit.variable = 'case_num',
                                 time.variable = 't',
                                 special.predictors = list(
                                   list("reg.civsoc.lnd", -1:-4, "mean"),
                                   list("reg.civsoc.lnd", -5:-8, "mean"),
                                   list("reg.civsoc.lnd", -9:-12, "mean"),
                                   list("v2xel_frefair", -1:-4, "mean"),
                                   list("v2xel_frefair", -5:-8, "mean"),
                                   list("v2xel_frefair", -9:-12, "mean"),
                                   list("v2xcs_ccsi", -1:-4, "mean"),
                                   list("v2xcs_ccsi", -5:-8, "mean"),
                                   list("v2xcs_ccsi", -9:-12, "mean")
                                 ),
                                 treatment.identifier = treatment.identifier,
                                 controls.identifier = as.integer(control_id),
                                 time.optimize.ssr = c(-12:-1),
                                 time.predictors.prior = c(-12:-1),
                                 unit.names.variable = 'cus_caseid',
                                 time.plot = c(-12:10)
)

synth.civsoc.Cambodia <- synth(data.civsoc.Cambodia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Cambodia, data.civsoc.Cambodia)


#------------------------------------
#China
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'China'|new_d$cus_caseid == 'China 1949-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'China 1949-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.China <- dataprep(new_d,
                              dependent = dep_var,
                              unit.variable = 'case_num',
                              time.variable = 't',
                              special.predictors = list(
                                list("reg.civsoc.lnd", -1:-4, "mean"),
                                list("reg.civsoc.lnd", -5:-8, "mean"),
                                list("reg.civsoc.lnd", -9:-12, "mean"),
                                list("v2xel_frefair", -1:-4, "mean"),
                                list("v2xel_frefair", -5:-8, "mean"),
                                list("v2xel_frefair", -9:-12, "mean"),
                                list("v2xcs_ccsi", -1:-4, "mean"),
                                list("v2xcs_ccsi", -5:-8, "mean"),
                                list("v2xcs_ccsi", -9:-12, "mean")
                              ),
                              treatment.identifier = treatment.identifier,
                              controls.identifier = as.integer(control_id),
                              time.optimize.ssr = c(-12:-1),
                              time.predictors.prior = c(-12:-1),
                              unit.names.variable = 'cus_caseid',
                              time.plot = c(-12:10)
)

synth.civsoc.China <- synth(data.civsoc.China, custom.v = rep(1, 9))
path.plot(synth.civsoc.China, data.civsoc.China)


#------------------------------------
#Cuba
pred_vars <- c(
  'v2xel_frefair'#,
  #		'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Cuba'|new_d$cus_caseid == 'Cuba 1959-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Cuba 1959-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Cuba <- dataprep(new_d,
                             dependent = dep_var,
                             unit.variable = 'case_num',
                             time.variable = 't',
                             special.predictors = list(
                               #list("reg.civsoc.lnd", -1:-4, "mean"),
                               #list("reg.civsoc.lnd", -5:-8, "mean"),
                               #list("reg.civsoc.lnd", -9:-12, "mean"),
                               list("v2xel_frefair", -1:-4, "mean"),
                               list("v2xel_frefair", -5:-8, "mean"),
                               list("v2xel_frefair", -9:-12, "mean"),
                               list("v2xcs_ccsi", -1:-4, "mean"),
                               list("v2xcs_ccsi", -5:-8, "mean"),
                               list("v2xcs_ccsi", -9:-12, "mean")
                             ),
                             treatment.identifier = treatment.identifier,
                             controls.identifier = as.integer(control_id),
                             time.optimize.ssr = c(-12:-1),
                             time.predictors.prior = c(-12:-1),
                             unit.names.variable = 'cus_caseid',
                             time.plot = c(-12:10)
)

synth.civsoc.Cuba <- synth(data.civsoc.Cuba, custom.v = rep(1, 6))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Cuba, data.civsoc.Cuba)


#------------------------------------
#Eritrea
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Eritrea'|new_d$cus_caseid == 'Eritrea 1993-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Eritrea 1993-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Eritrea <- dataprep(new_d,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                special.predictors = list(
                                  #list("reg.civsoc.lnd", -1:-4, "mean"),
                                  #list("reg.civsoc.lnd", -5:-8, "mean"),
                                  #list("reg.civsoc.lnd", -9:-12, "mean"),
                                  list("v2xel_frefair", -1:-4, "mean"),
                                  list("v2xel_frefair", -5:-8, "mean"),
                                  list("v2xel_frefair", -9:-12, "mean"),
                                  list("v2xcs_ccsi", -1:-4, "mean"),
                                  list("v2xcs_ccsi", -5:-8, "mean"),
                                  list("v2xcs_ccsi", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-12:-1),
                                time.predictors.prior = c(-12:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-12:10)
)

synth.civsoc.Eritrea <- synth(data.civsoc.Eritrea, custom.v = rep(1, 6))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Eritrea, data.civsoc.Eritrea)

#------------------------------------
#Guinea-Bissau
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Guinea-Bissau'|new_d$cus_caseid == 'Guinea-Bissau 1974-1999',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Guinea-Bissau 1974-1999']); treatment.identifier
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.GuineaBissau <- dataprep(new_d,
                                     dependent = dep_var,
                                     unit.variable = 'case_num',
                                     time.variable = 't',
                                     special.predictors = list(
                                       #list("reg.civsoc.lnd", -1:-4, "mean"),
                                       #list("reg.civsoc.lnd", -5:-8, "mean"),
                                       #list("reg.civsoc.lnd", -9:-12, "mean"),
                                       list("v2xel_frefair", -1:-4, "mean"),
                                       list("v2xel_frefair", -5:-8, "mean"),
                                       list("v2xel_frefair", -9:-12, "mean"),
                                       list("v2xcs_ccsi", -1:-4, "mean"),
                                       list("v2xcs_ccsi", -5:-8, "mean"),
                                       list("v2xcs_ccsi", -9:-12, "mean")
                                     ),
                                     treatment.identifier = treatment.identifier,
                                     controls.identifier = as.integer(control_id),
                                     time.optimize.ssr = c(-12:-1),
                                     time.predictors.prior = c(-12:-1),
                                     unit.names.variable = 'cus_caseid',
                                     time.plot = c(-12:10)
)

synth.civsoc.GuineaBissau <- synth(data.civsoc.GuineaBissau, custom.v = rep(1, 6))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.GuineaBissau, data.civsoc.GuineaBissau)


#------------------------------------
#Iran
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Iran'|new_d$cus_caseid == 'Iran 1979-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Iran 1979-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Iran <- dataprep(new_d,
                             dependent = dep_var,
                             unit.variable = 'case_num',
                             time.variable = 't',
                             special.predictors = list(
                               list("reg.civsoc.lnd", -1:-4, "mean"),
                               list("reg.civsoc.lnd", -5:-8, "mean"),
                               list("reg.civsoc.lnd", -9:-12, "mean"),
                               list("v2xel_frefair", -1:-4, "mean"),
                               list("v2xel_frefair", -5:-8, "mean"),
                               list("v2xel_frefair", -9:-12, "mean"),
                               list("v2xcs_ccsi", -1:-4, "mean"),
                               list("v2xcs_ccsi", -5:-8, "mean"),
                               list("v2xcs_ccsi", -9:-12, "mean")
                             ),
                             treatment.identifier = treatment.identifier,
                             controls.identifier = as.integer(control_id),
                             time.optimize.ssr = c(-12:-1),
                             time.predictors.prior = c(-12:-1),
                             unit.names.variable = 'cus_caseid',
                             time.plot = c(-12:10)
)

synth.civsoc.Iran <- synth(data.civsoc.Iran, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Iran, data.civsoc.Iran)


#------------------------------------
#Mexico
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Mexico'|new_d$cus_caseid == 'Mexico 1915-2000',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Mexico 1915-2000'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Mexico <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("reg.civsoc.lnd", -1:-4, "mean"),
                                 list("reg.civsoc.lnd", -5:-8, "mean"),
                                 list("reg.civsoc.lnd", -9:-12, "mean"),
                                 list("v2xel_frefair", -1:-4, "mean"),
                                 list("v2xel_frefair", -5:-8, "mean"),
                                 list("v2xel_frefair", -9:-12, "mean"),
                                 list("v2xcs_ccsi", -1:-4, "mean"),
                                 list("v2xcs_ccsi", -5:-8, "mean"),
                                 list("v2xcs_ccsi", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)


synth.civsoc.Mexico <- synth(data.civsoc.Mexico, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Mexico, data.civsoc.Mexico)

#------------------------------------
#Mozambique
pred_vars <- c(
  #'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)
#omit v2xel_frefair because data are missing over the pre-treatment period
dat[dat$Country=='Mozambique', c('Year', 'v2xel_frefair', 'v2xcs_ccsi', 'reg.civsoc.lnd')]

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Mozambique'|new_d$cus_caseid == 'Mozambique 1975-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Mozambique 1975-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Mozambique <- dataprep(new_d,
                                   dependent = dep_var,
                                   unit.variable = 'case_num',
                                   time.variable = 't',
                                   #predictors = c('v2xel_frefair', 'reg.civsoc.lnd'),
                                   special.predictors = list(
                                     #list("reg.civsoc.lnd", -1:-4, "mean"),
                                     #list("reg.civsoc.lnd", -5:-8, "mean"),
                                     #list("reg.civsoc.lnd", -9:-12, "mean"),
                                     #list("v2xel_frefair", -1:-4, "mean"),
                                     #list("v2xel_frefair", -5:-8, "mean"),
                                     #list("v2xel_frefair", -9:-12, "mean"),
                                     list("v2xcs_ccsi", -1:-4, "mean"),
                                     list("v2xcs_ccsi", -5:-8, "mean"),
                                     list("v2xcs_ccsi", -9:-12, "mean")
                                   ),
                                   treatment.identifier = treatment.identifier,
                                   controls.identifier = as.integer(control_id),
                                   time.optimize.ssr = c(-12:-1),
                                   time.predictors.prior = c(-8:-1),
                                   unit.names.variable = 'cus_caseid',
                                   time.plot = c(-12:10)
)

synth.civsoc.Mozambique <- synth(data.civsoc.Mozambique, custom.v = rep(1, 3))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Mozambique, data.civsoc.Mozambique)

#------------------------------------
#Nicaragua
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Nicaragua'|new_d$cus_caseid == 'Nicaragua 1979-1990',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Nicaragua 1979-1990'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Nicaragua <- dataprep(new_d,
                                  dependent = dep_var,
                                  unit.variable = 'case_num',
                                  time.variable = 't',
                                  special.predictors = list(
                                    list("reg.civsoc.lnd", -1:-4, "mean"),
                                    list("reg.civsoc.lnd", -5:-8, "mean"),
                                    list("reg.civsoc.lnd", -9:-12, "mean"),
                                    list("v2xel_frefair", -1:-4, "mean"),
                                    list("v2xel_frefair", -5:-8, "mean"),
                                    list("v2xel_frefair", -9:-12, "mean"),
                                    list("v2xcs_ccsi", -1:-4, "mean"),
                                    list("v2xcs_ccsi", -5:-8, "mean"),
                                    list("v2xcs_ccsi", -9:-12, "mean")
                                  ),
                                  treatment.identifier = treatment.identifier,
                                  controls.identifier = as.integer(control_id),
                                  time.optimize.ssr = c(-12:-1),
                                  time.predictors.prior = c(-12:-1),
                                  unit.names.variable = 'cus_caseid',
                                  time.plot = c(-12:10)
)

synth.civsoc.Nicaragua <- synth(data.civsoc.Nicaragua, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Nicaragua, data.civsoc.Nicaragua)

#------------------------------------
#Rwanda
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Rwanda'|new_d$cus_caseid == 'Rwanda 1994-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Rwanda 1994-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Rwanda <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("reg.civsoc.lnd", -1:-4, "mean"),
                                 list("reg.civsoc.lnd", -5:-8, "mean"),
                                 list("reg.civsoc.lnd", -9:-12, "mean"),
                                 list("v2xel_frefair", -1:-4, "mean"),
                                 list("v2xel_frefair", -5:-8, "mean"),
                                 list("v2xel_frefair", -9:-12, "mean"),
                                 list("v2xcs_ccsi", -1:-4, "mean"),
                                 list("v2xcs_ccsi", -5:-8, "mean"),
                                 list("v2xcs_ccsi", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.civsoc.Rwanda <- synth(data.civsoc.Rwanda, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Rwanda, data.civsoc.Rwanda)


#------------------------------------
#Russia
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
new_d <- new_d[new_d$Country != 'Russia'|new_d$cus_caseid == 'Russia 1917-1991',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Russia 1917-1991'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Russia <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("reg.civsoc.lnd", -1:-4, "mean"),
                                 list("reg.civsoc.lnd", -5:-8, "mean"),
                                 list("reg.civsoc.lnd", -9:-12, "mean"),
                                 list("v2xel_frefair", -1:-4, "mean"),
                                 list("v2xel_frefair", -5:-8, "mean"),
                                 list("v2xel_frefair", -9:-12, "mean"),
                                 list("v2xcs_ccsi", -1:-4, "mean"),
                                 list("v2xcs_ccsi", -5:-8, "mean"),
                                 list("v2xcs_ccsi", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.civsoc.Russia <- synth(data.civsoc.Russia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Russia, data.civsoc.Russia)

#------------------------------------
#Vietnam
pred_vars <- c(
  'v2xel_frefair'#,
  #'reg.civsoc.lnd'
)

dat[dat$Country=='Vietnam', c('Year', 'v2xel_frefair', 'reg.civsoc.lnd', 'v2xcs_ccsi')]

new_d <- balance_dataset(dset, pred_vars, dep_var, include_9_12 = F)$d
new_d <- new_d[new_d$Country != 'Vietnam'|new_d$cus_caseid == 'Vietnam 1954-',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Vietnam 1954-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Vietnam <- dataprep(new_d,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                special.predictors = list(
                                  #list("reg.civsoc.lnd", -1:-4, "mean"),
                                  #list("reg.civsoc.lnd", -5:-8, "mean"),
                                  #list("reg.civsoc.lnd", -9:-12, "mean"),
                                  list("v2xel_frefair", -1:-4, "mean"),
                                  list("v2xel_frefair", -5:-8, "mean"),
                                  #list("v2xel_frefair", -9:-12, "mean"),
                                  list("v2xcs_ccsi", -1:-4, "mean"),
                                  list("v2xcs_ccsi", -5:-8, "mean")
                                  #list("v2xcs_ccsi", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-8:-1),
                                time.predictors.prior = c(-8:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-8:10)
)

synth.civsoc.Vietnam <- synth(data.civsoc.Vietnam, custom.v = rep(1, 4))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Vietnam, data.civsoc.Vietnam)

#------------------------------------
#Yugoslavia
pred_vars <- c(
  'v2xel_frefair',
  'reg.civsoc.lnd'
)
dat[dat$Country=='Yugoslavia', c('ccode', 'Year', 'v2xel_frefair', 'reg.civsoc.lnd', 'v2xcs_ccsi')]

#D[D$Country=='Italy', c('Year', 'v2xel_frefair', 'reg.civsoc.lnd', 'civsoc')]


new_d <- balance_dataset(dset, pred_vars, dep_var, include_9_12 = T)$d
new_d <- new_d[new_d$Country != 'Yugoslavia'|new_d$cus_caseid == 'Yugoslavia 1945-1990',]
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Yugoslavia 1945-1990'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.civsoc.Yugoslavia <- dataprep(new_d,
                                   dependent = dep_var,
                                   unit.variable = 'case_num',
                                   time.variable = 't',
                                   special.predictors = list(
                                     list("reg.civsoc.lnd", -1:-4, "mean"),
                                     list("reg.civsoc.lnd", -5:-8, "mean"),
                                     list("reg.civsoc.lnd", -9:-12, "mean"),
                                     list("v2xel_frefair", -1:-4, "mean"),
                                     list("v2xel_frefair", -5:-8, "mean"),
                                     list("v2xel_frefair", -9:-12, "mean"),
                                     list("v2xcs_ccsi", -1:-4, "mean"),
                                     list("v2xcs_ccsi", -5:-8, "mean"),
                                     list("v2xcs_ccsi", -9:-12, "mean")
                                   ),
                                   treatment.identifier = treatment.identifier,
                                   controls.identifier = as.integer(control_id),
                                   time.optimize.ssr = c(-12:-1),
                                   time.predictors.prior = c(-12:-1),
                                   unit.names.variable = 'cus_caseid',
                                   time.plot = c(-12:10)
)

synth.civsoc.Yugoslavia <- synth(data.civsoc.Yugoslavia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.civsoc.Yugoslavia, data.civsoc.Yugoslavia)


##################################################################
#Military size
##################################################################
dep_var <- 'milperpc'

dset$milperpc <- dset$milper/dset$tpop
dset$ln.milperpc <- log(dset$milper/dset$tpop)
for (lag in 1:15){
  dset[paste('milperpc', 'L', lag, sep = '')] <- dset[paste('milper', 'L', lag, sep = '')]/dset[paste('tpop', 'L', lag, sep = '')]
  dset[paste('ln.milperpc', 'L', lag, sep = '')] <- log(dset[paste('milper', 'L', lag, sep = '')]/dset[paste('tpop', 'L', lag, sep = '')])
}
for (lead in 1:15){
  dset[paste('milperpc', 'Lead', lead, sep = '')] <- dset[paste('milper', 'Lead', lead, sep = '')]/dset[paste('tpop', 'Lead', lead, sep = '')]
  dset[paste('ln.milperpc', 'Lead', lead, sep = '')] <- log(dset[paste('milper', 'Lead', lead, sep = '')]/dset[paste('tpop', 'Lead', lead, sep = '')])
}

#------------------------------------
#Bolivia
pred_vars <- c(
  'war',
  'e_migdppcln'
)

#Interpolate one missing observation (missing military size for year 1943) for Bolivia
dset[dset$cus_caseid == 'Bolivia 1952-1964' & dset$Year == 1953, c('Year', 'milperpcL10')] <- (dset[dset$cus_caseid == 'Bolivia 1952-1964' & dset$Year == 1953, c('Year', 'milperpcL9')] + dset[dset$cus_caseid == 'Bolivia 1952-1964' & dset$Year == 1953, c('Year', 'milperpcL11')])/2

new_d <- balance_dataset(d=dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Bolivia 1952-1964'])
treatment.identifier
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])


data.milper.Bolivia <- dataprep(new_d,
                                dependent = dep_var,
                                unit.variable = 'case_num',
                                time.variable = 't',
                                special.predictors = list(
                                  list("war", -1:-4, "mean"),
                                  list("war", -5:-8, "mean"),
                                  list("war", -9:-12, "mean"),
                                  list("e_migdppcln", -1:-4, "mean"),
                                  list("e_migdppcln", -5:-8, "mean"),
                                  list("e_migdppcln", -9:-12, "mean"),
                                  list("milperpc", -1:-4, "mean"),
                                  list("milperpc", -5:-8, "mean"),
                                  list("milperpc", -9:-12, "mean")
                                ),
                                treatment.identifier = treatment.identifier,
                                controls.identifier = as.integer(control_id),
                                time.optimize.ssr = c(-12:-1),
                                time.predictors.prior = c(-12:-1),
                                unit.names.variable = 'cus_caseid',
                                time.plot = c(-12:10)
)

synth.milper.Bolivia <- synth(data.milper.Bolivia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Bolivia, data.milper.Bolivia)

#------------------------------------
#Cambodia
pred_vars <- c(
  'war',
  'e_migdppcln'
)


new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Cambodia 1975-1979'])
treatment.identifier
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Cambodia <- dataprep(new_d,
                                 dependent = dep_var,
                                 unit.variable = 'case_num',
                                 time.variable = 't',
                                 special.predictors = list(
                                   list("war", -1:-4, "mean"),
                                   list("war", -5:-8, "mean"),
                                   list("war", -9:-12, "mean"),
                                   list("e_migdppcln", -1:-4, "mean"),
                                   list("e_migdppcln", -5:-8, "mean"),
                                   list("e_migdppcln", -9:-12, "mean"),
                                   list("milperpc", -1:-4, "mean"),
                                   list("milperpc", -5:-8, "mean"),
                                   list("milperpc", -9:-12, "mean")
                                 ),
                                 treatment.identifier = treatment.identifier,
                                 controls.identifier = as.integer(control_id),
                                 time.optimize.ssr = c(-12:-1),
                                 time.predictors.prior = c(-12:-1),
                                 unit.names.variable = 'cus_caseid',
                                 time.plot = c(-12:10)
)

synth.milper.Cambodia <- synth(data.milper.Cambodia, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Cambodia, data.milper.Cambodia)

#------------------------------------
#China
pred_vars <- c(
  'war'#,
  #'e_migdppcln'# GDP series incomplete
)
dep_var <- 'milperpc'
dat[dat$ Country=='China', c('Year', 'e_migdppcln')]
new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'China 1949-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.China <- dataprep(new_d,
                              dependent = dep_var,
                              unit.variable = 'case_num',
                              time.variable = 't',
                              special.predictors = list(
                                list("war", -1:-4, "mean"),
                                list("war", -5:-8, "mean"),
                                list("war", -9:-12, "mean"),
                                #list("e_migdppcln", -1:-4, "mean"),
                                #list("e_migdppcln", -5:-8, "mean"),
                                #list("e_migdppcln", -1:-12, "mean"),
                                list("milperpc", -1:-4, "mean"),
                                list("milperpc", -5:-8, "mean"),
                                list("milperpc", -9:-12, "mean")
                              ),
                              treatment.identifier = treatment.identifier,
                              controls.identifier = as.integer(control_id),
                              time.optimize.ssr = c(-12:-1),
                              time.predictors.prior = c(-12:-1),
                              unit.names.variable = 'cus_caseid',
                              time.plot = c(-12:10)
)

synth.milper.China <- synth(data.milper.China, custom.v = rep(1, 6))
path.plot(synth.milper.China, data.milper.China)

#------------------------------------
#Cuba
pred_vars <- c(
  'war',
  'e_migdppcln'
)
dep_var <- 'milperpc'

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Cuba 1959-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Cuba <- dataprep(new_d,
                             dependent = dep_var,
                             unit.variable = 'case_num',
                             time.variable = 't',
                             special.predictors = list(
                               list("war", -1:-4, "mean"),
                               list("war", -5:-8, "mean"),
                               list("war", -9:-12, "mean"),
                               list("e_migdppcln", -1:-4, "mean"),
                               list("e_migdppcln", -5:-8, "mean"),
                               list("e_migdppcln", -9:-12, "mean"),
                               list("milperpc", -1:-4, "mean"),
                               list("milperpc", -5:-8, "mean"),
                               list("milperpc", -9:-12, "mean")
                             ),
                             treatment.identifier = treatment.identifier,
                             controls.identifier = as.integer(control_id),
                             time.optimize.ssr = c(-12:-1),
                             time.predictors.prior = c(-12:-1),
                             unit.names.variable = 'cus_caseid',
                             time.plot = c(-12:10)
)

synth.milper.Cuba <- synth(data.milper.Cuba, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Cuba, data.milper.Cuba)

#------------------------------------
#Iran
pred_vars <- c(
  'war',
  'e_migdppcln'
)
new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Iran 1979-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Iran <- dataprep(new_d,
                             dependent = dep_var,
                             unit.variable = 'case_num',
                             time.variable = 't',
                             special.predictors = list(
                               list("war", -1:-4, "mean"),
                               list("war", -5:-8, "mean"),
                               list("war", -9:-12, "mean"),
                               list("e_migdppcln", -1:-4, "mean"),
                               list("e_migdppcln", -5:-8, "mean"),
                               list("e_migdppcln", -9:-12, "mean"),
                               list("milperpc", -1:-4, "mean"),
                               list("milperpc", -5:-8, "mean"),
                               list("milperpc", -9:-12, "mean")
                             ),
                             treatment.identifier = treatment.identifier,
                             controls.identifier = as.integer(control_id),
                             time.optimize.ssr = c(-12:-1),
                             time.predictors.prior = c(-12:-1),
                             unit.names.variable = 'cus_caseid',
                             time.plot = c(-12:10)
)

synth.milper.Iran <- synth(data.milper.Iran, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Iran, data.milper.Iran)

#------------------------------------
#Mexico
pred_vars <- c(
  'war',
  'e_migdppcln'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Mexico 1915-2000'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

dat[dat$Country=='Mexico', c('Year', 'war', 'e_migdppcln', 'milper')]

data.milper.Mexico <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("war", -1:-4, "mean"),
                                 list("war", -5:-8, "mean"),
                                 list("war", -9:-12, "mean"),
                                 list("e_migdppcln", -1:-4, "mean"),
                                 list("e_migdppcln", -5:-8, "mean"),
                                 list("e_migdppcln", -9:-12, "mean"),
                                 list("milperpc", -1:-4, "mean"),
                                 list("milperpc", -5:-8, "mean"),
                                 list("milperpc", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.milper.Mexico <- synth(data.milper.Mexico, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Mexico, data.milper.Mexico)


#------------------------------------
#Nicaragua
pred_vars <- c(
  'war',
  'e_migdppcln'
)

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Nicaragua 1979-1990'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Nicaragua <- dataprep(new_d,
                                  dependent = dep_var,
                                  unit.variable = 'case_num',
                                  time.variable = 't',
                                  special.predictors = list(
                                    list("war", -1:-4, "mean"),
                                    list("war", -5:-8, "mean"),
                                    list("war", -9:-12, "mean"),
                                    list("e_migdppcln", -1:-4, "mean"),
                                    list("e_migdppcln", -5:-8, "mean"),
                                    list("e_migdppcln", -9:-12, "mean"),
                                    list("milperpc", -1:-4, "mean"),
                                    list("milperpc", -5:-8, "mean"),
                                    list("milperpc", -9:-12, "mean")
                                  ),
                                  treatment.identifier = treatment.identifier,
                                  controls.identifier = as.integer(control_id),
                                  time.optimize.ssr = c(-12:-1),
                                  time.predictors.prior = c(-12:-1),
                                  unit.names.variable = 'cus_caseid',
                                  time.plot = c(-12:10)
)

synth.milper.Nicaragua <- synth(data.milper.Nicaragua, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Nicaragua, data.milper.Nicaragua)

#------------------------------------
#Rwanda
pred_vars <- c(
  'war',
  'e_migdppcln'
)
dep_var <- 'milperpc'

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Rwanda 1994-'])
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Rwanda <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("war", -1:-4, "mean"),
                                 list("war", -5:-8, "mean"),
                                 list("war", -9:-12, "mean"),
                                 list("e_migdppcln", -1:-4, "mean"),
                                 list("e_migdppcln", -5:-8, "mean"),
                                 list("e_migdppcln", -9:-12, "mean"),
                                 list("milperpc", -1:-4, "mean"),
                                 list("milperpc", -5:-8, "mean"),
                                 list("milperpc", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.milper.Rwanda <- synth(data.milper.Rwanda, custom.v = rep(1, 9))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Rwanda, data.milper.Rwanda)

#------------------------------------
#Russia
pred_vars <- c(
  'war'#,
  #'e_migdppcln' #incomplete GDP data
)


dset[dset$cus_caseid == 'Russia 1917-1991' & dset$Year == 1918, c('Year', 'milperpc')] <- (dset[dset$cus_caseid == 'Russia 1917-1991' & dset$Year == 1918, c('Year', 'milperpcL1')] + dset[dset$cus_caseid == 'Russia 1917-1991' & dset$Year == 1918, c('Year', 'milperpcLead1')])/2

new_d <- balance_dataset(dset, pred_vars, dep_var)$d
treatment.identifier <- unique(new_d$case_num[new_d$cus_caseid == 'Russia 1917-1991'])
treatment.identifier
rev.identifiers <- unique(new_d$case_num[new_d$cus_caseid %in% revregimes])
control_id <- levels(as.factor(new_d$case_num))
control_id <- as.character(control_id[!(control_id %in% c(treatment.identifier, rev.identifiers))])

data.milper.Russia <- dataprep(new_d,
                               dependent = dep_var,
                               unit.variable = 'case_num',
                               time.variable = 't',
                               special.predictors = list(
                                 list("war", -1:-4, "mean"),
                                 list("war", -5:-8, "mean"),
                                 list("war", -9:-12, "mean"),
                                 #list("e_migdppcln", -1:-4, "mean"),
                                 #list("e_migdppcln", -5:-8, "mean"),
                                 #list("e_migdppcln", -9:-12, "mean"),
                                 list("milperpc", -1:-4, "mean"),
                                 list("milperpc", -5:-8, "mean"),
                                 list("milperpc", -9:-12, "mean")
                               ),
                               treatment.identifier = treatment.identifier,
                               controls.identifier = as.integer(control_id),
                               time.optimize.ssr = c(-12:-1),
                               time.predictors.prior = c(-12:-1),
                               unit.names.variable = 'cus_caseid',
                               time.plot = c(-12:10)
)

synth.milper.Russia <- synth(data.milper.Russia, custom.v = rep(1, 6))#, optimxmethod = 'L-BFGS-B')
path.plot(synth.milper.Russia, data.milper.Russia)


save(
  synth.civsoc.Afghanistan, data.civsoc.Afghanistan, 
  synth.civsoc.Albania, data.civsoc.Albania, 
  synth.civsoc.Algeria, data.civsoc.Algeria, 
  synth.civsoc.Angola, data.civsoc.Angola, 
  synth.civsoc.Bolivia, data.civsoc.Bolivia, 
  synth.civsoc.Cambodia, data.civsoc.Cambodia, 
  synth.civsoc.China, data.civsoc.China, 
  synth.civsoc.Cuba, data.civsoc.Cuba, 
  synth.civsoc.Eritrea, data.civsoc.Eritrea, 
  synth.civsoc.GuineaBissau, data.civsoc.GuineaBissau, 
  synth.civsoc.Iran, data.civsoc.Iran, 
  synth.civsoc.Mexico, data.civsoc.Mexico, 
  synth.civsoc.Mozambique, data.civsoc.Mozambique, 
  synth.civsoc.Nicaragua, data.civsoc.Nicaragua, 
  synth.civsoc.Russia, data.civsoc.Russia, 
  synth.civsoc.Rwanda, data.civsoc.Rwanda, 
  synth.civsoc.Vietnam, data.civsoc.Vietnam, 
  synth.civsoc.Yugoslavia, data.civsoc.Yugoslavia, 
  synth.milper.Cambodia, data.milper.Cambodia, 
  synth.milper.Cuba, data.milper.Cuba, 
  synth.milper.China, data.milper.China, 
  synth.milper.Iran, data.milper.Iran, 
  synth.milper.Nicaragua, data.milper.Nicaragua, 
  synth.milper.Rwanda, data.milper.Rwanda, 
  file = 'synth.Rdata')

